/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.whu.weibo_souhu.login;

import com.sun.net.ssl.SSLContext;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.ArrayList;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import org.whu.weibo_souhu.login.Cookie;
import org.whu.weibo_souhu.login.LoginInfo;
import org.whu.weibo_souhu.util.URL;
/**
 *
 * @author Administrator
 */
public class SouhuLogin {
    
    public LoginInfo logininfo;
    public Cookie cookie;
    
    public HashMap hm=new HashMap();
    
    public SouhuLogin(LoginInfo logininfo)
    {
        this.logininfo=logininfo;
    }
    
    private static String getTime()
    {
        long servertime=new Date().getTime()/1;
        return String.valueOf(servertime);
    }
    
    public Cookie GetCookie() throws IOException, NoSuchAlgorithmException, KeyManagementException
    {
        HttpClient httpclient=new DefaultHttpClient();
        HttpClientParams.setCookiePolicy(httpclient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);  //设置兼容性
        httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,1000); //设置连接时限。请求时间
        httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000); //设置读取时间
    //    httpclient=WebClientDevWrapper.wrapClient(httpclient);
        HttpClient httpsClient = HttpsClient.getInstance();
        HttpGet httpget=new HttpGet("http://t.sohu.com/new_index");
        HttpResponse response=httpclient.execute(httpget);
        if(response.getStatusLine().getStatusCode()==200)
        {
            Header[] header=response.getHeaders("Set-Cookie");
            String params=new String();
            String value=new String();
            for(Header h:header)  //我从这里面获得了访问时间
            {
                params=h.getValue().substring(0,h.getValue().indexOf("="));
                value=h.getValue().substring(h.getValue().indexOf("=")+1,h.getValue().indexOf(";"));
                hm.put(params,value);
            }   
         }
    
        String userid=URLEncoder.encode(logininfo.username, "UTF-8");
        String pwd=new MD5().getMD5(logininfo.password);
        String servertime=getTime();
        String url="https://passport.sohu.com/sso/login.jsp?userid="+userid+"&password="+pwd+"&appid=9999&persistentcookie=0&s="+servertime+"&b=6&w=1536&pwdtype=1&v=26";
        httpget=new HttpGet(url);
        
       response=httpsClient.execute(httpget);
       if(response.getStatusLine().getStatusCode()==200)
       {
           Header[] header=response.getHeaders("Set-Cookie");
           String params=new String();
           String value=new String();
           for(Header h:header)
           {
               params=h.getValue().substring(0,h.getValue().indexOf("="));
               value=h.getValue().substring(h.getValue().indexOf("=")+1,h.getValue().indexOf(";"));
               hm.put(params, value);
           }
       }
       String mycookie="";
     //  mycookie ="SUV="+hm.get("SUV")+";IPLOC="+hm.get("IPLOC")+";pprdig="+hm.get("pprdig")+";ppmdig="+hm.get("ppmdig")+";ppinfo="+hm.get("ppinfo")+";ppinf="+hm.get("ppinf")+";passport="+hm.get("passport")+";lastdomain="+hm.get("lastdomain")+";JSESSIONID="+hm.get("JSESSIONID")+";IPLOC="+hm.get("IPLOC");
       
       Set<Entry<String, String>> entrySet = hm.entrySet();  
        // 遍历Set集合中的每一个Entry对象  
        Iterator<Entry<String, String>> it = entrySet.iterator();  
        Entry<String, String> entry;
        while(it.hasNext())
        {
            entry=it.next();
 //           mycookie=mycookie+entry.getKey()+"="+entry.getValue()+";";
            mycookie=mycookie.concat(entry.getKey()).concat("=").concat(entry.getValue()).concat(";");
        }
       
      
       System.out.println(mycookie);
       Cookie ck = new Cookie();
       ck.setCookie(mycookie);
       this.cookie=ck;
       return ck;
    }
    
    public boolean Connected()
    {
        try {
            this.cookie=this.GetCookie();
        } catch (IOException ex) {
            Logger.getLogger(SouhuLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(SouhuLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (KeyManagementException ex) {
            Logger.getLogger(SouhuLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(cookie!=null)
            return true;
        else
            return false;
    }
    
    
    public static void main(String[] agrs) throws Exception
    {
        String keywords="青山爆炸";
        keywords=URLEncoder.encode(URLEncoder.encode(keywords, "UTF-8"), "UTF-8");
        LoginInfo logininfo=new LoginInfo("guo.guowww@163.com","ximo128504");
        SouhuLogin mysouhu=new SouhuLogin(logininfo);
        Cookie cookie=mysouhu.GetCookie();
        String url="http://www.baidu.com";
        String result=new String();
        if(mysouhu.Connected())
        {
            result=new URL(url,cookie).gethtml();
           
            System.out.print(result.getBytes("utf-8"));
        }
       
    }
    
    
    
    
}
